package cn.oi.klittle.era.widget.viewpager

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.ViewPager
import cn.oi.klittle.era.base.KBaseView
import cn.oi.klittle.era.comm.kpx
import cn.oi.klittle.era.utils.KLoggerUtils
import cn.oi.klittle.era.utils.KProportionUtils

/**
 * viewpager下移动的圆点;圆点之间的距离会根据控件的大小，自由平均分配。
 */
class KDotView : KBaseView {

    constructor(viewGroup: ViewGroup) : super(viewGroup.context) {
        setLayerType(View.LAYER_TYPE_HARDWARE, null)
        viewGroup.addView(this)//直接添加进去,省去addView(view)
    }

    constructor(viewGroup: ViewGroup, HARDWARE: Boolean) : super(viewGroup.context) {
        if (HARDWARE) {
            setLayerType(View.LAYER_TYPE_HARDWARE, null)
        } else {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        }
        viewGroup.addView(this)//直接添加进去,省去addView(view)
    }

    //默认开启硬件加速
    constructor(context: Context?, HARDWARE: Boolean = true) : super(context, HARDWARE) {}

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    //默认圆点颜色
    var defaultColor = Color.parseColor("#9FA1A0")

    fun defaultColor(defaultColor: Int = this.defaultColor): KDotView {
        this.defaultColor = defaultColor
        return this
    }

    ///fixme 默认圆点，是否为空心样式。
    var isStrokeWithDefault = false;
    var strokeWidth = kpx.x(2f)

    //选择圆点颜色
    var selectColor = Color.parseColor("#7160C6")

    fun selectColor(selectColor: Int = this.selectColor): KDotView {
        this.selectColor = selectColor
        return this
    }

    ///FIXME 选中圆点是否为长条。
    var isStripWithSelect = false;

    var radius = KProportionUtils.getInstance().adapterInt(15 / 2)//圆点半径
    fun radius(radius: Int = this.radius): KDotView {
        this.radius = radius
        return this
    }

    var offset = KProportionUtils.getInstance().adapterInt(30)//圆点之间的间隙
    fun offset(offset: Int = this.offset): KDotView {
        this.offset = offset
        return this
    }

    private var viewPager: ViewPager? = null
    private var viewPagerOffset: Float = 0f
    var count: Int? = 0;//fixme 指定viewPager具体的个数，用于实现无限循环。

    ///绑定 viewPager
    ///count;指定viewPager的个数；如果viewPager的个数无限大（无限循环），需要指定具体的个数。fixme 无限大时，必须指明具体的个数。
    fun viewPager(viewPager: ViewPager?, count: Int? = null): KDotView {
        this.viewPager = viewPager
        this.count = count;
        viewPager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                var position = position;
                if (count != null && count != 0) {
                    position = position % count!!;
                    selectPosition = selectPosition % count!!;
                }
                //position 指向滑动页面的下标
                //positionOffset 偏移量，0到1之间
                if (position >= selectPosition) {
                    //向前
                    viewPagerOffset = (offset + radius * 2) * positionOffset
                } else if (position < selectPosition) {
                    //向后
                    viewPagerOffset = (offset + radius * 2) * (1 - positionOffset)
                    viewPagerOffset *= -1
                }
                if (count != null && selectPosition == 0 && (position + 1) == count) {
                    viewPagerOffset = 0f;
                }
                //KLoggerUtils.e("position:\t$position\tselectPosition:\t$selectPosition\tviewPagerOffset:\t$viewPagerOffset");
                invalidate()
            }

            override fun onPageSelected(position: Int) {
                var position = position;
                if (count != null && count != 0) {
                    position = position % count!!;
                    selectPosition = selectPosition % count!!;
                }
                viewPagerOffset = 0f
                selectPosition(position)
            }
        })
        return this
    }

    private var selectPosition = 0//选中下标
    fun selectPosition(selectPosition: Int = this.selectPosition): KDotView {
        this.selectPosition = selectPosition
        if (count != null && count != 0) {
            this.selectPosition = this.selectPosition % count!!;
        }
        viewPagerOffset = 0f
        invalidate()
        return this
    }

    //当前选中的x,y坐标
    var cx = 0f
    var cy = 0f
    override fun draw2(canvas: Canvas, paint: Paint) {
        super.draw2(canvas, paint)
        viewPager?.let {
            var count = it.adapter?.count//圆点个数
            if (this.count != null && this.count != 0) {
                count = this.count;
                this.selectPosition = this.selectPosition % this.count!!;
            }
            //KLoggerUtils.e("selectPosition:\t$selectPosition\tviewPagerOffset:\t$viewPagerOffset");
            count?.let {
                canvas.run {
                    var w = radius * count + offset * (count - 1)//圆点+间隙总长度
                    var x = (width - w) / 2 + radius//第一个圆点的x坐标
                    var y = height / 2
                    if (isStrokeWithDefault) {
                        paint.style = Paint.Style.STROKE
                        paint.strokeWidth = strokeWidth;
                    } else {
                        paint.style = Paint.Style.FILL_AND_STROKE
                        paint.strokeWidth = 0f;
                    }
                    for (i in 1..count) {
                        //画普通的点
                        paint.color = defaultColor
                        drawDefault?.let {
                            it(canvas, paint, x.toFloat(), y.toFloat())
                        }
                        if (drawDefault == null && i != selectPosition + 1) {
                            drawCircle(x.toFloat(), y.toFloat(), radius.toFloat(), paint)
                        }

                        if (i == selectPosition + 1) {
                            //选中点的坐标
                            cx = x.toFloat() + viewPagerOffset
                            cy = y.toFloat()
                        }

                        x += radius + offset + radius
                    }
                    //画选中点
                    paint.style = Paint.Style.FILL_AND_STROKE
                    paint.strokeWidth = 0f;
                    paint.color = selectColor
                    drawSelect?.let {
                        it(canvas, paint, cx, cy)
                    }
                    if (drawSelect == null) {
                        if (isStripWithSelect) {
                            ///fixme 长条
                            var left = cx - radius - offset / 2
                            var top = cy - radius
                            var right = cx + radius + offset / 2
                            var bottom = cy + radius
                            drawRoundRect(RectF(left, top, right, bottom), radius.toFloat(), radius.toFloat(), paint)
                        } else {
                            ///fixme 圆点
                            drawCircle(cx, cy, radius.toFloat(), paint)
                        }
                    }
                }
            }

        }
    }

    protected var drawDefault: ((canvas: Canvas, paint: Paint, x: Float, y: Float) -> Unit)? = null

    //fixme 自定义绘制普通圆点,返回的是圆心坐标点。
    fun drawDefault(drawDefault: (canvas: Canvas, paint: Paint, x: Float, y: Float) -> Unit) {
        this.drawDefault = drawDefault
    }

    protected var drawSelect: ((canvas: Canvas, paint: Paint, x: Float, y: Float) -> Unit)? = null

    //fixme 自定义绘制选中圆点,返回的是圆心坐标点。
    fun drawSelect(drawSelect: (canvas: Canvas, paint: Paint, x: Float, y: Float) -> Unit) {
        this.drawSelect = drawSelect
    }
}